Skip to content

Fix program lifetime and .ptd loading bugs in Module - #25412

Open
shoumikhin wants to merge 4 commits into
pytorch:mainfrom
shoumikhin:module-lifetime-and-ptd-fixes
Open

shoumikhin wants to merge 4 commits into
pytorch:mainfrom
shoumikhin:module-lifetime-and-ptd-fixes

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

What is wrong today

  • Module::program() returns a shared Program, but the data loader that program reads from belongs to the Module. When the Module is destroyed first, the program keeps a pointer to a deleted loader. The next segment it loads on demand, such as delegate data read by a later load_method(), is a use after free.
  • BundledModule::from_file frees the file bytes in its destructor, but a Program kept from program() still reads them.
  • A Module made from a shared Program and a data_map_loader never loads that data map, so methods that need the .ptd weights fail to load.
  • When load() fails on a missing .ptd file, a bad data map, or a bad program, a retried load() adds the earlier .ptd data a second time and fails with a duplicate key error.

What this changes

There is one commit per fix, in this order:

  1. The program returned by program() now owns its loader. It is released with the last strong owner of the program.
  2. from_file keeps the file bytes in a shared pointer that the Module's loader shares. The constructor that takes a caller's buffer is unchanged. bundled_module.h keeps the same public API. The bundled module test is now also registered in CMake when devtools are built.
  3. Both load paths now call one data map helper, which does nothing once the data maps are loaded. Behavior change: for a Module made from a shared program, a bad .ptd now fails load() and the calls that depend on it, such as method_meta(), num_methods(), load_method() and execute(), also for methods that need no external data. The helper also stops a retry after a bad program from adding the .ptd data again.
  4. load() opens every .ptd file before it keeps any, and rebuilds its data maps on each try, so a retry adds each file once.

What was tested

  • Built and ran the full C++ test suite on Linux x86_64 (CMake, Debug).
  • These new tests fail without their fix: TestProgramKeepsItsLoaderAlive, TestSharedProgramLoadsItsDataMap, TestLoadRetriesAfterAFailedProgramLoad and TestLoadRetriesAfterAMissingDataFile.
  • The new TestProgramOutlivesModuleFromFile can pass by luck, because it reads freed memory, so it was run under Valgrind: reads of freed memory without the fix, none with it. The one error left, Invalid alignment value: 0, comes from a moved-from FileDataLoader. That is a separate bug, fixed in a separate pull request.

Module::program() returns a shared program, and its documentation says
the data loader the program reads from stays valid for the lifetime of the
program. But the loader belonged to the Module. Once the Module was
destroyed, a program still held elsewhere kept a pointer to the deleted
loader and used it whenever it loaded a segment on demand, for example
when a later load_method() read the delegate data of a method from the file. That is a use after free.

The program returned by program() now owns its loader. The loader is
released together with the program, also when weak pointers to the program
remain. Modules that are destroyed together with their program behave as
before.
BundledModule::from_file reads the whole file into memory and gives the
Module a loader that only borrows those bytes. The module frees them in
its destructor. Module::program() hands out a shared pointer to the
Program, and that Program keeps reading the same bytes. So a caller that
keeps the Program after the BundledModule is gone reads freed memory.

from_file now keeps the file data in a shared pointer, and gives the
Module a SharedPtrDataLoader that shares it. The data lives as long as
either the module or the Program needs it. The public constructor that
takes a caller's buffer is unchanged: the caller still owns that buffer.

The CMake build now also runs the bundled module test when devtools are
built.
Module(std::shared_ptr<Program>, ..., data_map_loader) stores the loader of
the program's external weights but never reads it. load() returns early
once a program is set, so the .ptd data is never loaded, and every method
that needs it fails to load. A program shared between Modules could
therefore not use external weights at all.

load() now loads the data maps of such a Module too. The data map code
moves into one helper, which both paths call and which does nothing once
the data maps are loaded. That also stops a load() retried after a failed
Program::load from adding the .ptd data a second time and then failing with
a duplicate key error instead of the program error.

For such a Module, a bad .ptd now fails, with the .ptd error: load(),
num_methods(), method_names(), method_meta(), format(), method(),
get_mem_planned_buffer_sizes(), load_method(), and the calls built on
load_method(), such as load_forward(), execute() and forward(). This includes methods that need no external data. is_loaded() is
already true, because the program itself is loaded. Modules that load
their own program are unchanged.

This builds on the change that keeps a Module's data loader alive as long
as its program.
Module::load opens the .ptd files it was given and adds each one to its
list of data map loaders as soon as it is open. When opening a later file
fails, or loading one of the data maps fails, the earlier entries stay. A
retried load() then adds them a second time, and merging the data maps
fails with a duplicate key error, even once the missing file exists.

load() now opens every file before it keeps any, forgets the file names
once the files are kept, and rebuilds its list of data maps on each
attempt. So a retry adds each file once. Successful first loads are
unchanged.

This builds on the change that moves the data map code into one helper.
@shoumikhin shoumikhin added the release notes: runtime Changes related to the core runtime which loads the program methods, initializes delegates, and runs label Nov 21, 2026
@pytorch-bot

pytorch-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25412

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 1 Pending

As of commit 2e7a4b9 with merge base ba0d826 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 21, 2026
Copilot AI balanced review requested due to automatic review settings November 21, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: runtime Changes related to the core runtime which loads the program methods, initializes delegates, and runs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants